Print one to n


Posted by Christy on 2022-04-19

Description: Write a function that accepts a number n and print number one to n

function print(n) {

}

print(1);
// 1

print(3);
// 1
// 2
// 3

print(9);
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9

Answer:

function print(n) {
  for (let i = 1; i <= n; i++) {
    console.log(i);
  }
}

print(1);
print(3);
print(9);









Related Posts

[FE301] React 基礎(Class component 版)先別急著學 React

[FE301] React 基礎(Class component 版)先別急著學 React

關於 React 小書:實作 Clock 並簡介 component lifecycle 裡各個階段的作用

關於 React 小書:實作 Clock 並簡介 component lifecycle 裡各個階段的作用

[ week 1 ] 相對 絕對 傻傻分不清楚

[ week 1 ] 相對 絕對 傻傻分不清楚


Comments